module.exports = {
    configureWebpack: {
        devServer: {
            // mock编写接口的地方
            before(app) {
                var userlist = [{
                    "id": 12,
                    "UserName": "deng",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "18888888888",
                    "Email": "405328555@qq.com",
                    "MgState": true
                }, {
                    "id": 11,
                    "UserName": "admib",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "18888888888",
                    "Email": "405328555@qq.com",
                    "MgState": true
                }, {
                    "id": 10,
                    "UserName": "deng",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "18888888888",
                    "Email": "405328555@qq.com",
                    "MgState": true
                }, {
                    "id": 8,
                    "UserName": "5555",
                    "Password": "555555",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13312311231",
                    "Email": "55@qq.com",
                    "MgState": true
                }, {
                    "id": 7,
                    "UserName": "3333",
                    "Password": "333333",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13388888888",
                    "Email": "33@qq.com",
                    "MgState": true
                }, {
                    "id": 6,
                    "UserName": "2222",
                    "Password": "222222",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13845644564",
                    "Email": "22@qq.com",
                    "MgState": true
                }, {
                    "id": 4,
                    "UserName": "deng",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "",
                    "Email": "",
                    "MgState": true
                }, {
                    "id": 3,
                    "UserName": "den",
                    "Password": "",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13812341235",
                    "Email": "888888@qq.com",
                    "MgState": true
                }, {
                    "id": 2,
                    "UserName": "admin",
                    "Password": "",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "",
                    "Mobile": "13812341888",
                    "Email": "288888@qq.com",
                    "MgState": true
                }, {
                    "id": 1,
                    "UserName": "deng",
                    "Password": "123456",
                    "CreateTime": "0001/1/1 星期一 0:00:00",
                    "RoleName": "超级管理员",
                    "Mobile": "13602221234",
                    "Email": "223456@qq.com",
                    "MgState": false
                }]

                //搜索栏数据请求
                app.get('/api/menu', (req, res) => {
                    res.json({

                        "status": "ok",
                        "data": [{
                            "id": 1,
                            "name": "用户管理",
                            "child": [{
                                "id": 1,
                                "name": "用户列表",
                                "path": "/home/users"
                            }]
                        }, {
                            "id": 2,
                            "name": "员工管理",
                            "child": [{
                                "id": 2,
                                "name": "员工列表",
                                "path": "/home/rote"
                            }, ]
                        }, {
                            "id": 3,
                            "name": "商品管理",
                            "child": [{
                                "id": 4,
                                "name": "商品列表",
                                "path": "/home/product"
                            }, {
                                "id": 5,
                                "name": "分类参数",
                                "path": "/home/userss"
                            }, {
                                "id": 6,
                                "name": "商品分类",
                                "path": "/home/producttype"
                            }]
                        }, {
                            "id": 4,
                            "name": "订单管理",
                            "child": []
                        }, {
                            "id": 5,
                            "name": "数据统计",
                            "child": []
                        }]
                    })
                })

                // 模拟 一个登录接口
                app.get('/api/login', (req, res) => {
                        const tokenStr = 'dhclass'
                        const {
                            username,
                            password
                        } = req.query
                        if (username == 'admin' && password == '123456' ||
                            username == 'admin2' && password == '123456'
                        ) {
                            res.json({
                                code: 1,
                                message: '登录成功',
                                token: tokenStr + 'username' + username + (new Date().getTime)
                            })
                        } else {
                            res.json({
                                code: 0,
                                message: '登录失败'
                            })
                        }
                    }),

                //分页显示用户数据
                app.get('/api/users', (req, res) => {
                    const {
                        query,
                        pagenum,
                        pagesize
                    } = req.query
                    var total = userlist.length
                    var newdatalist = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
                    // if(query != ''){
                    //     var reg = new RegExp(query)
                    //     var newdatalist = newdatalist.filter(item=>{
                    //         return reg.test(item.UserName) || reg.test(item.Mobile)
                    //     })
                    // }  
                    res.json({
                        'status': 'ok',
                        'total': total,
                        'data': newdatalist
                    })
                })
                //修改用户状态的接口
                app.get('/api/userstate', (req, res) => {
                    const {
                        id,
                        MgState
                    } = req.query
                    var newState = ''
                    if (userlist) {
                        var user = userlist.filter(item => {
                            return item.id == id
                        })

                        user.MgState = MgState
                        newState = user.MgState
                    }
                    res.json({
                        "code": '200',
                        'newState': newState
                    })
                })
                //搜索用户接口
                app.get('/api/searchuesr', (req, res) => {
                    const {
                        query
                    } = req.query
                    var newDataList = []
                    if (query != "") {
                        var reg = new RegExp(query)
                        newDataList = userlist.filter(item => {
                            // return item.UserName == query || item.Mobile == query  
                            return reg.test(item.UserName) || reg.test(item.Mobile)
                        })
                    }
                    res.json({
                        'status': 'ok',
                        'data': newDataList
                    })
                })
                //添加用户接口
                app.get('/api/adduser', (req, res) => {
                    const { username, password, email, mobile,pagenum, pagesize } = req.query
                    var id = userlist[0].id
                    var adduser = {
                        "id": id + 1,
                        "UserName": username,
                        "Password": password,
                        "CreateTime": "0001/1/1 星期一 0:00:00",
                        "RoleName": "超级管理员",
                        "Mobile": mobile,
                        "Email": email,
                        "MgState": true
                    }
                    userlist.unshift(adduser)
                    
                    total = userlist.length
                    var newDataList = []
                   
                        newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
                        //向前端反馈的数据
                    res.json({
                        'total': total,
                        'data': newDataList
                    })
                })
                //修改用户接口
                app.get('/api/updateuser', (req, res) => {
                    const { id, username, password, email, mobile,pagenum, pagesize } = req.query
                    if (userlist) {
                        userlist.filter(item => item.id == id).forEach(element => {
                            element.UserName = username
                            element.Email = email
                            element.Mobile = mobile
                        })
                    }
                    var total = userlist.length
                    newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
                    res.json({
                        'status': 'ok',
                        'total': total,
                        'data': newDataList
                    })
                })
                // 删除用户的接口  
                app.get('/api/deleteuser',(req,res)=>{
                    const {id,query,pagenum,pagesize} = req.query 
                    for (var i = 0; i <userlist.length; i++)  {
                        if(userlist[i].id == id ) {
                            userlist.splice(i,1)
                        }
                    }
                    var total = userlist.length
                    newDataList = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
                    res.json({
                        'total': total,
                        'data': newDataList
                    })
                })
            }
        }
    }
}